package org.example.pmsystem.controller;

import org.example.pmsystem.Response;
import org.example.pmsystem.entity.Attend;
import org.example.pmsystem.entity.Salary;
import org.example.pmsystem.service.SalaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin(origins = "http://localhost:5173")
@RequestMapping("/salary")
public class SalaryController {
    @Autowired
    private SalaryService salaryService;


    @GetMapping("getSalaryBySno/{sno}")
    public Response<List<Salary>> getSalaryBySno(@PathVariable String sno){
        return Response.newSuccess(salaryService.selectSalaryBySno(sno));
    }

    @GetMapping("getSalaryByStatus/{status}")
    public Response<List<Salary>> getSalaryByStatus(@PathVariable String status){
        return Response.newSuccess(salaryService.selectSalaryByStatus(status));
    }

    @GetMapping("getSalaryByDate/{date}")
    public Response<List<Salary>> getSalaryByDate(@PathVariable String date){
        return Response.newSuccess(salaryService.selectSalaryByDate(date));
    }

    @PostMapping("calculateSalary")
    public Response<Boolean> calculateSalary(){
        if (salaryService.calculateSalary())
            return Response.newSuccess(true);
        return Response.newFail("部分员工基础薪资信息不全，无法完全计算！");
    }

    @PutMapping("updateSalary")
    public Response<String> updateSalary(@RequestBody Salary salary){
        salaryService.updateSalary(salary);
        return Response.newSuccess("薪资数据已更新，请重新核算！");
    }

    @PutMapping("updateSalaryStatus")
    public Response<Boolean> updateSalaryStatus(@RequestBody Salary salary){
        salaryService.updateSalaryStatus(salary);
        return Response.newSuccess(true);
    }

    @PostMapping("insertAttend")
    public Response<Boolean> insertAttend(@RequestBody Attend attend) {
        if (salaryService.insertAttend(attend))
            return Response.newSuccess(true);
        return Response.newFail("该记录已存在，请勿重复打卡");
    }
}
